package com.cetccloud.ap.proxy.channel.cossDomain.s30.handler.send;

import com.alibaba.fastjson.JSON;
import com.cetccloud.ap.proxy.channel.cossDomain.s30.S30Config;
import com.cetccloud.ap.proxy.channel.cossDomain.s30.S30Exception;
import com.cetccloud.ap.proxy.channel.cossDomain.s30.channel.send.S30SendChannel;
import com.cetccloud.ap.util.IdUtil;
import com.thunisoft.exchange.gateway.protocol.channel.GatewayEntityMessage;
import com.thunisoft.exchange.gateway.protocol.channel.GatewayMetadataRequestHeader;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @ClassName MetaReqHandler
 * @Description 名录请求
 * @Author superz
 * @Date 2021/4/22 20:52
 * @Version 1.0
 **/
@Slf4j
@Component
public class MetaReqHandler {

    @Autowired
    private S30Config s30Config;

    @Autowired
    private S30SendChannel s30SendChannel;

    public void handle() throws S30Exception {
        GatewayEntityMessage entityMessage = new GatewayEntityMessage();
        GatewayMetadataRequestHeader requestHeader = new GatewayMetadataRequestHeader();
        requestHeader.setRequestID(IdUtil.requestId());
        // 可选字段，填写应用系统标识，如未填写，则返回域中所有名录信息；否则返回与该应用系统具有收发权限的名录信息
//        requestHeader.setAppID(s30Config.getProxy().getAppId());
        entityMessage.setHeader(requestHeader);
        log.info("主动请求名录数据请求... 请求: {}", JSON.toJSONString(entityMessage));

        boolean flag = s30SendChannel.asyncSend(entityMessage);
        if (!flag) {
            throw new S30Exception("S30Channel.asyncSend failed");
        }
    }
}
